<?php $this->load->view('templates/backend/header'); ?>
<h3><?php echo $this->lang->line("voucher_list");?></h3>
<style>
    .num_cls{
        width:20%;
    }
    .btn-success,.btn-danger{
        vertical-align: top;
    }
    .notice{
        color:green;
    }
    .navbar-inverse{
        display: none;
    }
    body{
        padding-top:10px;
        padding-bottom:30px;
    }
</style>

<div class="well">
    <?php echo form_open();?>
        <div class="tab-content">	
            <div class="tab-pane active" id="main">
                <?php
                //thông tin khách hàng
                echo form_label($this->lang->line('phone'), 'phone');
                echo form_input(array('name' => 'phone', 'id' => 'phone', 'value' => ($info->phone)?$info->phone:'','required'=>'required','class'=>'num_cls','placeholder'=>$this->lang->line('phone'),'class'=>'num_cls','readonly' => 'true'));
                echo ' <span id="findCustomer"></span>';
                echo form_error('phone');
                //mã đặt chỗ
                echo form_label($this->lang->line('booking_place_code'), 'booking_place_code');
                echo form_input(array('name' => 'booking_place_code', 'id' => 'booking_place_code', 'value' => ($info->pnr)?$info->pnr:'','required'=>'required','placeholder'=>$this->lang->line('booking_place_code'),'readonly' => 'true'));
                echo ' <span id="findCustomer"></span>';
                echo form_error('booking_place_code');
                //Giá net
                echo form_label($this->lang->line('sumNet').' <span class="required">*</span>', 'net_fare');
                echo form_input(array('name' => 'net_fare', 'id' => 'net_fare', 'value' => $info->net_fare,'required'=>'required','readonly' => 'true'));
                echo form_error('net_fare');
                echo form_input(array('type' => 'hidden','name' => 'net_fare_fake', 'id' => 'net_fare_fake', 'value' => $info->net_fare));
                //Giá bán
                echo form_label($this->lang->line('sumSale').' <span class="required">*</span>', 'sale_fare');
                echo form_input(array('name' => 'sale_fare', 'id' => 'sale_fare', 'value' => $info->sale_fare,'required'=>'required','readonly' => 'true'));
                echo form_error('sale_fare');
                echo form_input(array('type' => 'hidden','name' => 'sale_fare_fake', 'id' => 'sale_fare_fake', 'value' => $info->sale_fare));
                //Số dư hiện tại
                //tạm thời cho Phong sửa số dư hiện tại đang thuận tiện việc chuyển số dư chung sang số dư riêng
                $sodu    =   $this->voucher_model->getSurplusCusABooker(array('customerID'=>$info->customerID,'owner'=>$info->owner));
                
//                if($sodu[0]->residual == ""){
//                    $sodu = $this->voucher_model->getInfoContactsByParams(array('key'=>'customerID','value'=>$info->customerID));
//                    $sodufull = $sodu->surplus;
//                } else {
                    $sodufull = $sodu[0]->residual;
//                }
                echo form_label($this->lang->line('surplus'), 'surplus');
                echo form_input(array('name' => 'surplus', 'id' => 'surplus', 'value' => ($sodufull)?$sodufull:'0','required'=>'required'));
                echo form_error('surplus');
                echo form_input(array('type' => 'hidden','name' => 'surplus_fake', 'id' => 'surplus_fake', 'value' => ($sodufull)?$sodufull:'0'));
                //Giá thực
                echo form_label($this->lang->line('real_fare').' <span class="required">*</span>', 'real_fare');
                echo form_input(array('name' => 'real_fare', 'id' => 'real_fare', 'value' => '','required'=>'required'));
                echo form_error('real_fare');
                echo form_input(array('type' => 'hidden','name' => 'real_fare_fake', 'id' => 'real_fare_fake', 'value' => ''));
                
                //Số dư còn lại
                $khautru    =   form_checkbox(array('name'=>'checkbox_khautru','id'=>'checkbox_khautru','value'=>0,'checked'=>FALSE,'style'=>'margin:10px;')).'<label style="display:inline-block" class="label" for="checkbox_khautru">'.$this->lang->line('checkbox_khautru').'</label> ';
                $sodukh     =   form_checkbox(array('name'=>'checkbox_sodukh','id'=>'checkbox_sodukh','value'=>0,'checked'=>FALSE,'style'=>'margin:10px;')).'<label style="display:inline-block" class="label" for="checkbox_sodukh">'.$this->lang->line('checkbox_sodukh').'</label>';
                if(isset($info->net_fare) && $info->net_fare < 0){
                    $khautru =   '';
                }
                echo form_input(array('type'=>'hidden','name' => 'checkbox_khautru_fake', 'id' => 'checkbox_khautru_fake', 'value' => 0));
                echo form_input(array('type'=>'hidden','name' => 'checkbox_sodukh_fake', 'id' => 'checkbox_sodukh_fake', 'value' => 0));
                
                echo form_label($this->lang->line('residual').$khautru.$sodukh, 'residual');
                echo form_input(array('name' => 'residual', 'id' => 'residual', 'value' => 0,'readonly'=>'true'));
                echo form_error('real_fare');
                echo form_input(array('type' => 'hidden','name' => 'residual_fake', 'id' => 'residual_fake', 'value' => 0));
                //định khoản
                echo form_label($this->lang->line('clause'), 'clause');
                echo form_input(array('name' => 'clause', 'id' => 'clause', 'value' => ''));
                echo form_input(array('type'=>'hidden','name' => 'clause_fake', 'id' => 'clause_fake', 'value' => ''));
                echo form_error('clause');
                //assign giao vé nếu có
                if(in_array($info->payment_method,array(3,4,5))){
                    echo form_label('Phát sinh giao vé', 'ticketworkers');
                    echo form_dropdown('ticketworkers',$this->voucher_model->getListTicketWorkers(),'','id="ticketworkers"');
                    echo form_error('ticketworkers');
                }
                //Ghi chú booker
                echo form_label($this->lang->line('note_bk'), 'note_bk');
                echo form_textarea(array('name' => 'note_bk', 'id' => 'note_bk', 'value' => ($info->note_bk)?$info->note_bk:'','rows'=>'5','readonly' => 'true'));
                echo form_error('note_bk');
                //Ghi chú payment
                echo form_label($this->lang->line('note_pay'), 'note_pay');
                echo form_textarea(array('name' => 'note_pay', 'id' => 'note_pay', 'value' => ($info->note_pay)?$info->note_pay:'','rows'=>'5'));
                echo form_error('note_pay');
                
                echo form_input(array('type' => 'hidden','name' => 'id', 'id' => 'id', 'value' => $info->id));
                echo form_input(array('type' => 'hidden','name' => 'customerID', 'id' => 'customerID', 'value' => $info->customerID));
                echo form_input(array('type' => 'hidden','name' => 'owner', 'id' => 'owner', 'value' => $info->owner));
                ?>
            </div>
        </div>
        <div class="top-button">
                <?php echo form_button(array('id' => 'submit', 'value' => 'Update', 'name' => 'submit', 'type' => 'submit', 'content' => $this->lang->line('save'),'class' => 'btn btn-primary')); ?>
                <?php echo form_button(array('id' => 'cancelSMS','content' => $this->lang->line('close'),'class' => 'btn btn-danger','onclick'=>'closeFancy()')); ?>
        </div>
    <?php form_close();?>
    <span id="simple-msg-submit"></span>
</div>

<?php $this->load->view('templates/backend/footer'); ?>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
    $(function() {
        var autocompleteData = <?php echo $arr_complete;?>;
 
        $('#clause').autocomplete({ 
         source: autocompleteData,
         change: function (event, ui) {
             if(ui.item !== null){
                document.getElementById("clause_fake").value             = ui.item.code;
             } else {
                document.getElementById("clause_fake").value             = '';
             }
         } 
        });
  });
</script>

<script src="<?php echo STATIC_URL.'backend/js/autoNumeric.js';?>"></script>
<script type="text/javascript">    
$(document).ready(function() {
        var net_fare = $("#net_fare").val();
        $('#net_fare').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
        $("#net_fare").blur(function() {
            document.getElementById("net_fare_fake").value = UnFormatNumber($('#net_fare').val());
        });
        $('#sale_fare').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
        $("#sale_fare").blur(function() {
            document.getElementById("sale_fare_fake").value = UnFormatNumber($('#sale_fare').val());
        });
        $('#real_fare').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
        $("#real_fare").blur(function() {
            document.getElementById("real_fare_fake").value = UnFormatNumber($('#real_fare').val());
            if(net_fare < 0){
                document.getElementById("residual").value       = FormatNumber(Number($('#real_fare_fake').val()) + Number($('#sale_fare_fake').val()));
                document.getElementById("residual_fake").value  = UnFormatNumber($('#residual').val());
            } else {
                document.getElementById("residual").value       = FormatNumber(Number($('#real_fare_fake').val()) - Number($('#sale_fare_fake').val()));
                document.getElementById("residual_fake").value  = UnFormatNumber($('#residual').val());
            }
        });
        $("#surplus").blur(function() {
            document.getElementById("surplus_fake").value = UnFormatNumber($('#surplus').val());
        });
        $('#surplus').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
        $('#residual').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
        
        $('#checkbox_khautru').on('click',function(){
            if ( $(this).is(':checked') ) {
                if($('#real_fare').val() == ""){alert("Vui lòng điền vào ô Giá thực");return false;}
                // Do stuff
                document.getElementById("residual").value                         = FormatNumber(Number($('#residual_fake').val()) + Number($("#surplus_fake").val()));
                document.getElementById("residual_fake").value                    = UnFormatNumber($('#residual').val());
                //surplus
                document.getElementById("surplus").value                          = 0;
                document.getElementById("surplus_fake").value                     = 0;
                //disable this
                $(this).attr("disabled", true);
                document.getElementById("checkbox_khautru_fake").value            = 1;
            }
        });
        
        $('#checkbox_sodukh').on('click',function(){
            if ( $(this).is(':checked') ) {
                //console.log($("#checkbox_khautru").is(':checked'));return false;
                //disable this
                $(this).attr("disabled", true);
                $("#real_fare").attr("readonly", true);
                if($("#checkbox_khautru").is(':checked') == false){
                    if($('#net_fare_fake').val() < 0){
                        document.getElementById("real_fare_fake").value                 = 0 - Number($('#sale_fare_fake').val());
                    } else {
                        document.getElementById("real_fare_fake").value                 = $('#sale_fare_fake').val();
                    }
                    document.getElementById("real_fare").value                      = FormatNumber($('#real_fare_fake').val());
                } else {
                    document.getElementById("real_fare_fake").value                 = $('#sale_fare_fake').val();
                    document.getElementById("real_fare").value                      = FormatNumber($('#real_fare_fake').val());
                }
                document.getElementById("checkbox_sodukh_fake").value           = 1;
            }
        });
});
function FormatNumber(x) {
    if (typeof x === "undefined") {
        return '';
    } else {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + ' VNĐ';
    }
};
function UnFormatNumber(x) {
    if (typeof x === "undefined") {
        return '';
    } else {
    return x.toString().replace(/,|VNĐ|\s/g, "");
    }
};
function closeFancy(){
    parent.jQuery.fancybox.close();
}
</script>